import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../router.dart';

class SettingPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState()=>_SettingPageState();
}

class _SettingPageState extends State<SettingPage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
          child: Padding(
            padding: EdgeInsets.only(top: 10.0),
            child: CustomScrollView(
              physics: const BouncingScrollPhysics(),
              shrinkWrap: false,
              slivers: <Widget>[
                SliverAppBar(
                  backgroundColor: Colors.transparent,
                  title: Text("设置"),
                  //expandedHeight: 200.0,
                ),
                _divider(),
                _personItem('账号',''),
                _small_divider(),
                _personItem('重置登录密码',''),
                _divider(),
                _personItem('关于',''),
                _small_divider(),
                _personItem('账号注销',''),
                _big_divider(),
                _logout('退出账号',onTab: (){
                  _logoutAction();
                }),
                _big_divider(),
                _version('堵嘴 1.0.1'),
              ],
            ),
          )),
    );
  }

  _logoutAction(){
    _clear();
    MyRouter.pushNoParams(context, MyRouter.loginPage);
  }

  _clear() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.clear();
  }

  SliverToBoxAdapter _personItem(String title, String name,
      { VoidCallback onTab}) {
    return SliverToBoxAdapter(
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(10.0),
              child: Text(
                title,
                style: TextStyle(fontSize: 15.0),
              ),
            ),
            Expanded(
              child: Text(
                name,
                style: TextStyle(fontSize: 15.0),
              ),
            ),
            _rightArrow()
          ],
        ),
        onTap: onTab,
      ),
    );
  }

  SliverToBoxAdapter _logout(String title,
      {VoidCallback onTab}) {
    return SliverToBoxAdapter(
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        child: Container(
          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
          child:
             Align(
              alignment: Alignment.center,
              child: Text(
                title,
                style: TextStyle(fontSize: 15.0,color: Colors.deepOrangeAccent),
              ),
            ),
        ),
        onTap: onTab,
      ),
    );
  }

  SliverToBoxAdapter _version(String title,
      {VoidCallback onTab}) {
    return SliverToBoxAdapter(
      child:
        Container(
          margin: const EdgeInsets.only(top: 10.0, bottom: 10.0),
          child:
          Align(
            alignment: Alignment.center,
            child: Text(
              title,
              style: TextStyle(fontSize: 15.0, color: Colors.black26),
            ),
          ),
        ),
    );
  }

  _rightArrow() {
    return Icon(
      Icons.chevron_right,
      color: const Color.fromARGB(255, 204, 204, 204),
    );
  }

  SliverToBoxAdapter _small_divider() {
    return SliverToBoxAdapter(
      child: Container(
        height: 1.0,
        color: const Color.fromARGB(255, 247, 247, 247),
      ),
    );
  }

  SliverToBoxAdapter _divider() {
    return SliverToBoxAdapter(
      child: Container(
        height: 10.0,
        color: const Color.fromARGB(255, 247, 247, 247),
      ),
    );
  }

  SliverToBoxAdapter _big_divider() {
    return SliverToBoxAdapter(
      child: Container(
        height: 15.0,
        color: const Color.fromARGB(255, 247, 247, 247),
      ),
    );
  }
}
